layui.use(['table','form', 'element'], function () {
    let table = layui.table,
        form = layui.form,
        $ = layui.jquery,
       element = layui.element;

    //方法级渲染
    table.render({
        elem: '#project'
        , url: '/admProject/page'
        , toolbar: '#toolbarTable'
        , title: '项目'
        , totalRow: true
        ,skin: 'line'
        , id: "project"
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'id', title: '项目ID', width: 100, sort: true, align: 'center'}
            , {field: 'projectName', title: '项目名称', width: 170, sort: true}
            , {field: 'prodWebUrl', title: '生产环境地址', width: 220,templet: function (d) {
                    var url = d.prodWebUrl==null?"":d.prodWebUrl
                    return '<a style="text-decoration:underline;" target="_blank" href="'+url+'"  >'+url+'</a>';
                }}
            , {field: 'gitlabProjectName', title: 'gitlab项目名称', width: 170, sort: true}
            , {field: 'developer', title: '订阅消息人员配置', width: 250, sort: true}
            , {field: 'remark', title: '备注', width: 160, sort: true}
            , {field: 'tagToProd', title: '打tag是否部署生产', width: 200,templet: function (d) {
                    var tagToProd = d.tagToProd==1?"是":"否"
                    return tagToProd;
                }}
            , {field: 'sort', title: '排序', width: 100, sort: true}

            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
        ]]
        , even: true
        , page: true //是否显示分页
        , skip: true //开启跳页
        , limits: [10, 30, 60]
        , width: 1700
        // , height: 'full-150'
        , limit: 10 //每页默认显示的数量
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "code": status, //解析接口状态   （好像必须是0 才可以）
                "msg": data.desc, //解析提示文本
                "count": data.total, //解析数据长度
                "data": data.records //解析数据列表
            };
        }
        ,done:function (res,currentCount) {
            //***重点***：table渲染完成后渲染element进度条
            element.render()
        }

    });


    //头工具栏事件
    table.on('toolbar(project)', function (obj) {
        let data = table.checkStatus(obj.config.id).data;
        switch (obj.event) {
            case 'add':
                add();
                break;
            case 'del':
                if (data.length==0){
                    layer.msg("请先勾选数据",{icon:2,time:3000});
                    return;
                }
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids+=data[i].id+","
                }
                ids = ids.substr(0,ids.length-1);
                del(ids);
                break;
        }
        ;
    });

    //监听行工具事件
    table.on('tool(project)', function (obj) {
        const data = obj.data;
        if (obj.event === 'update'){
            update(data);
        }else if (obj.event === 'del'){
            del(data.id)
        }
    });

    function del(ids) {
        layer.confirm("确认要删除吗，删除后不能恢复", { title: "删除确认" }, function (index) {
            $.ajax({
                type: "delete",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "/admProject/"+ids,//url
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg(result.data,{icon:1,time:1000},function () {
                            location.reload();
                        });
                    };
                },
                error: function () {
                    alert("异常！");
                }
            });
        })
    }

    function add() {
        layer.open({
            type: 2,
            title: '新增项目',
            area: ['700px', '750px'],
            fix: false, //
            closeBtn: false,
            shift: 2,
            shadeClose: false,
            content: ["add.html", 'no'],
            success: function (layero, index) {
                form.render();
            },
            yes: function () {
            }
        });
    }
    function update(data) {
        layer.open({
            type: 2,
            title: '修改项目',
            area: ['700px', '750px'],
            fix: false, //
            closeBtn: false,
            shift: 2,
            shadeClose: false,
            content: ["update.html", 'no'],
            success: function (layero, index) {
                const iframeWin = layero.find('iframe')[0].contentWindow;
                const body = layer.getChildFrame('body', index);//获取弹出层的dom元素
                body.find("#id").val(data.id);
                body.find("#projectName").val(data.projectName);
                body.find("#sort").val(data.sort);
                body.find("#remark").val(data.remark);
                body.find("#gitlabProjectName").val(data.gitlabProjectName);
                body.find("#gitUrl").val(data.gitUrl);
                body.find("#httpUrl").val(data.httpUrl);
                body.find("#teamMembersMailId").val(data.teamMembersMail);
                body.find("#prodWebUrl").val(data.prodWebUrl);
                body.find("#tagToProd").val(data.tagToProd);
                body.find("#folder").val(data.folder);
                iframeWin.layui.form.render();
            },
            yes: function () {
            }
        });
    }

    $('#query').click(function(){
        search();
    });
    $(document).keyup(function(event){
        if(event.keyCode ==13){
            search();
        }
    });
    function search(){
        const content = $('#content');
        //执行重载
        table.reloadData('project', {
            method: 'get',
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                content: content.val(),
                serverId: $('#serverId').val(),
            }
        }, 'data');
    }
});